1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.collect;
18
19 import static com.google.common.base.Preconditions.checkArgument;
20
21 import com.google.common.annotations.GwtCompatible;
22 import com.google.common.annotations.GwtIncompatible;
23 import com.google.common.collect.MapConstraintsTest.TestKeyException;
24 import com.google.common.collect.MapConstraintsTest.TestValueException;
25 import com.google.common.collect.testing.features.CollectionFeature;
26 import com.google.common.collect.testing.features.CollectionSize;
27 import com.google.common.collect.testing.features.MapFeature;
28 import com.google.common.collect.testing.google.BiMapTestSuiteBuilder;
29 import com.google.common.collect.testing.google.TestStringBiMapGenerator;
30
31 import junit.framework.Test;
32 import junit.framework.TestCase;
33 import junit.framework.TestSuite;
34
35 import java.util.Map.Entry;
36
37
38
39
40
41
42
43 @GwtCompatible(emulated = true)
44 public class ConstrainedBiMapTest extends TestCase {
45
46 private static final String TEST_KEY = "42";
47 private static final String TEST_VALUE = "test";
48 private static final MapConstraint<String, String> TEST_CONSTRAINT = new TestConstraint();
49
50 @GwtIncompatible("suite")
51 public static Test suite() {
52 TestSuite suite = new TestSuite();
53 suite.addTest(BiMapTestSuiteBuilder
54 .using(new ConstrainedBiMapGenerator())
55 .named("Maps.constrainedBiMap[HashBiMap]")
56 .withFeatures(
57 CollectionSize.ANY,
58 CollectionFeature.SUPPORTS_ITERATOR_REMOVE,
59 MapFeature.ALLOWS_NULL_KEYS,
60 MapFeature.ALLOWS_NULL_VALUES,
61 MapFeature.ALLOWS_ANY_NULL_QUERIES,
62 MapFeature.GENERAL_PURPOSE,
63 MapFeature.REJECTS_DUPLICATES_AT_CREATION)
64 .createTestSuite());
65 suite.addTestSuite(ConstrainedBiMapTest.class);
66 return suite;
67 }
68
69 public void testPutWithForbiddenKeyForbiddenValue() {
70 BiMap<String, String> map = MapConstraints.constrainedBiMap(
71 HashBiMap.<String, String> create(),
72 TEST_CONSTRAINT);
73 try {
74 map.put(TEST_KEY, TEST_VALUE);
75 fail("Expected IllegalArgumentException");
76 } catch (IllegalArgumentException expected) {
77
78 }
79 }
80
81 public void testPutWithForbiddenKeyAllowedValue() {
82 BiMap<String, String> map = MapConstraints.constrainedBiMap(
83 HashBiMap.<String, String> create(),
84 TEST_CONSTRAINT);
85 try {
86 map.put(TEST_KEY, "allowed");
87 fail("Expected IllegalArgumentException");
88 } catch (IllegalArgumentException expected) {
89
90 }
91 }
92
93 public void testPutWithAllowedKeyForbiddenValue() {
94 BiMap<String, String> map = MapConstraints.constrainedBiMap(
95 HashBiMap.<String, String> create(),
96 TEST_CONSTRAINT);
97 try {
98 map.put("allowed", TEST_VALUE);
99 fail("Expected IllegalArgumentException");
100 } catch (IllegalArgumentException expected) {
101
102 }
103 }
104
105 public static final class ConstrainedBiMapGenerator extends TestStringBiMapGenerator {
106 @Override
107 protected BiMap<String, String> create(Entry<String, String>[] entries) {
108 BiMap<String, String> bimap = MapConstraints.constrainedBiMap(
109 HashBiMap.<String, String> create(),
110 TEST_CONSTRAINT);
111 for (Entry<String, String> entry : entries) {
112 checkArgument(!bimap.containsKey(entry.getKey()));
113 bimap.put(entry.getKey(), entry.getValue());
114 }
115 return bimap;
116 }
117 }
118
119 private static final class TestConstraint implements MapConstraint<String, String> {
120 @Override
121 public void checkKeyValue(String key, String value) {
122 if (TEST_KEY.equals(key)) {
123 throw new TestKeyException();
124 }
125 if (TEST_VALUE.equals(value)) {
126 throw new TestValueException();
127 }
128 }
129
130 private static final long serialVersionUID = 0;
131 }
132 }